package com.pinea;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class Pinea {

    public static final int PINEA_EVENT_QUIT = 0x100;
    public static final int PINEA_EVENT_KEY_DOWN = 0x300;
    public static final int PINEA_EVENT_KEY_UP = 0x301;
    public static final int PINEA_EVENT_MOUSE_MOTION = 0x400;
    public static final int PINEA_EVENT_MOUSE_BUTTON_DOWN = 0x401;
    public static final int PINEA_EVENT_MOUSE_BUTTON_UP = 0x402;
    public static final int PINEA_EVENT_MOUSE_WHEEL = 0x403;

    public static final int PINEA_KEY_SPACE = 0x00000020;
    public static final int PINEA_KEY_A = 0x00000061;
    public static final int PINEA_KEY_D = 0x00000064;
    public static final int PINEA_KEY_S = 0x00000073;
    public static final int PINEA_KEY_W = 0x00000077;
    public static final int PINEA_KEY_H = 0x00000068;
    public static final int PINEA_KEY_J = 0x0000006a;
    public static final int PINEA_KEY_K = 0x0000006b;
    public static final int PINEA_KEY_L = 0x0000006c;

    public static final int PINEA_FLIP_HORIZONTAL = 0x00000001;
    public static final int PINEA_FLIP_VERTICAL = 0x00000002;

    // PineaLib
    private static final PineaLib lib;

    static {
        System.setProperty("jna.library.path", "Pinea/Pinea/bin");
        lib = Native.load("Pinea", PineaLib.class);
    }

    public static void pineaInit(String title, int width, int height) {
        lib.pineaInit(title, width, height);
    }

    public static void pineaQuit() {
        lib.pineaQuit();
    }

    public static void pineaShow() {
        lib.pineaShow();
    }

    public static boolean pineaPollEvent(PineaEvent event) {
        return lib.pineaPollEvent(event);
    }

    public static void pineaClear(PineaColor color) {
        lib.pineaClear(color);
    }

    public static void pineaRender() {
        lib.pineaRender();
    }

    public static void pineaFillRect(PineaRect rect, PineaColor color) {
        lib.pineaFillRect(rect, color);
    }

    public static PineaImage pineaLoadImage(String path) {
        Pointer pointer = lib.pineaLoadImage(path);
        if (pointer == Pointer.NULL) {
            return null;
        }
        return new PineaImage(pointer);
    }

    public static void pineaDrawImage(PineaImage image, PineaRect srcRect, PineaRect dstRect, double angle, int flip) {
        if (image != null) {
            lib.pineaDrawImage(image.pointer, srcRect, dstRect, angle, flip);
        }
    }

    public static void pineaDropImage(PineaImage image) {
        if (image != null) {
            lib.pineaDropImage(image.pointer);
        }
    }

    // mp3 or ogg 格式
    public static PineaAudio pineaLoadMusic(String path) {
        Pointer pointer = lib.pineaLoadMusic(path);
        if (pointer == Pointer.NULL) {
            return null;
        }
        return new PineaAudio(pointer);
    }

    public static void pineaPlayMusic(PineaAudio audio, boolean loop) {
        if (audio != null) {
            lib.pineaPlayMusic(audio.pointer, loop);
        }
    }

    public static void pineaPauseMusic() {
        lib.pineaPauseMusic();
    }

    public static void pineaResumeMusic() {
        lib.pineaResumeMusic();
    }

    public static void pineaStopMusic() {
        lib.pineaStopMusic();
    }

    public static void pineaDropMusic(PineaAudio audio) {
        if (audio != null) {
            lib.pineaDropMusic(audio.pointer);
        }
    }

    // wav格式
    public static PineaAudio pineaLoadSound(String path) {
        Pointer pointer = lib.pineaLoadSound(path);
        if (pointer == Pointer.NULL) {
            return null;
        }
        return new PineaAudio(pointer);
    }

    public static void pineaPlaySound(PineaAudio audio, int loops) {
        lib.pineaPlaySound(audio.pointer, loops);
    }

    public static void pineaDropSound(PineaAudio audio) {
        if (audio != null) {
            lib.pineaDropSound(audio.pointer);
        }
    }

    public static long pineaGetTime() {
        return lib.pineaGetTime();
    }
}
